package cn.hfuu.dao.impl;

import cn.hfuu.dao.UserDao;
import cn.hfuu.domain.User;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import java.util.List;

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

    public User getUserByUserCode(String userCode) {

       return getHibernateTemplate().execute(new HibernateCallback<User>(){

            @Override
            public User doInHibernate(Session session) throws HibernateException {
                String sql = "from User where user_code=?";

                Query query = session.createQuery(sql);
                query.setParameter(0,userCode);
                User user = (User) query.uniqueResult();
                return user;

            }
        });

        /*
        DetachedCriteria dc = DetachedCriteria.forClass(User.class);
        dc.add(Restrictions.eq("user_code",userCode));

        List<User> list = (List<User>) getHibernateTemplate().findByCriteria(dc);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        else{
            return null;
        }*/
    }
}
